package cn.yitun.chat.yitunserverimapicore.business.chat.service.impl;

import cn.yitun.chat.yitunservercommonlib.converter.ot.UserChatHistoryToLastChatUserConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.LastChatUser;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.UserChatHistory;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.LastChatDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.userchat.LastChatInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.LastChatVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserChatContentVo;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.UserChatTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.LastChatUserService;
import cn.yitun.chat.yitunserverimbase.dao.userchat.LastChatDao;
import cn.yitun.chat.yitunserverimbase.dao.userchat.UserChatContentDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager.UserChatHistoryManager;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Date 2019-03-08 10:06:45<br>
 * Description
 *
 * @author yitun<br>
 * @version 1.1
 */
@Service
public class LastChatUserServiceImpl
		extends CrudServiceImpl<LastChatDao, LastChatDto, LastChatInfo, LastChatVo>
		implements LastChatUserService {

//	@Resource
//	UserChatItemDao userChatItemDao;
	@Resource
	UserChatContentDao userChatContentDAO;
	@Resource
	UserChatHistoryManager userChatHistoryManager;

	/**
	 * 
	 * Date 2019-03-08 10:06:30<br>
	 * Description 获取最后聊天的列表
	 * 
	 * @param lastChatDto
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public List<LastChatUser> getLastChatWithContentList(LastChatDto lastChatDto) {
		int pageNo = lastChatDto.getCurrentPageNo();
		int pageSize = lastChatDto.getPageSize();
		Long userId = lastChatDto.getUserId();

		PageHelper.startPage(pageNo, pageSize);
		List<LastChatVo> list = dao.listVO(
				R.param("userId", userId)
				.put("type", UserChatTypeEnums.TYPE_USER.getType())
				);
		List<Long> idList = new ArrayList<>();
		for (LastChatVo ulc : list) {
			idList.add(ulc.getContentId());
		}
		List<UserChatContentVo> userContentList = userChatContentDAO.listVO(
				R.param("ids", idList)
				);
		List<UserChatHistory> contents = userChatHistoryManager.getUserChatHistoryList(userContentList, true);

		return UserChatHistoryToLastChatUserConverter.INSTANCE.historyToPo(contents);
	}

	/**
	 * 删除最后聊天记录
	 * @param userId
	 * @param chatId
	 * @return
	 */
	@Transactional
	public boolean remove(Long userId, Long chatId) {
		boolean mark = 0 < dao.deleteBy(userId, chatId, UserChatTypeEnums.TYPE_USER.getType());
		return mark;
	}

	/**
	 * 删除最后聊天记录
	 * @param userId
	 * @param chatId
	 * @param type
	 */
	@Override
	@Transactional
	public void remove(Long userId, Long chatId, String type) {
		dao.deleteBy(userId, chatId, type);
	}
}
